package com.oilsection.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oilsection.demo.controller.req.ZrdImportReq;
import com.oilsection.demo.controller.req.ZrdPageQueryReq;
import com.oilsection.demo.controller.res.JcEventRightPanelResponse;
import com.oilsection.demo.controller.res.ZrdPageQueryResponse;
import com.oilsection.demo.dao.GetJcdListSourceDao;
import com.oilsection.demo.dao.GetJzListSourceDao;
import com.oilsection.demo.dao.GetQkListSourceDao;
import com.oilsection.demo.mapper.JcEventMapper;
import com.oilsection.demo.mapper.ZrdMapper;
import com.oilsection.demo.po.JcEvent;
import com.oilsection.demo.po.JcdInfoTable;
import com.oilsection.demo.po.KqInfoTable;
import com.oilsection.demo.po.Zrd;
import com.oilsection.demo.service.JcEventService;
import com.oilsection.demo.service.ZrdService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@RequiredArgsConstructor
public class JcEventServiceImpl extends ServiceImpl<JcEventMapper, JcEvent> implements JcEventService {
    private final GetQkListSourceDao kqDao;

    private final GetJzListSourceDao jzDao;

    private final GetJcdListSourceDao jcdDao;

    @Override
    public List<JcEventRightPanelResponse> list(JcEvent req) {
        List<JcEventRightPanelResponse> responses = new ArrayList<>();

        List<JcEvent> list = query().eq(ObjUtil.isNotEmpty(req.getJzId()), "jz_id", req.getJzId())
                .eq(ObjUtil.isNotEmpty(req.getJcType()), "jc_type", req.getJcType())
                .eq(ObjUtil.isNotEmpty(req.getKqId()), "kq_id", req.getKqId())
                .list();

        for (JcEvent jcEvent : list) {
            JcEventRightPanelResponse response = new JcEventRightPanelResponse();
            response.setJcEventId(jcEvent.getId());

            KqInfoTable kqInfoTable = kqDao.findById(jcEvent.getKqId()).orElse(null);
            response.setKqName(kqInfoTable.getName());

            response.setJzName(jzDao.findById(jcEvent.getJzId()).orElse(null).getJzName());

            JcdInfoTable jcdInfo = jcdDao.findById(jcEvent.getJcdId()).orElse(null);
            response.setJcdName(jcdInfo.getJcdName());

            response.setJcTime(DateUtil.format(jcEvent.getJcTime(), "yyyy-MM-dd"));

            response.setJcValue(jcEvent.getJcValue());

            response.setJcXpoint(jcdInfo.getJcdXpoint());

            response.setJcYpoint(jcdInfo.getJcdYpoint());

            responses.add(response);
        }

        return responses;
    }

    @Override
    public List<JcEventRightPanelResponse> listEvents(JcEvent req) {
        List<JcEventRightPanelResponse> responses = new ArrayList<>();

        List<JcEvent> list = query().eq(ObjUtil.isNotEmpty(req.getJcdId()), "jcd_id", req.getJcdId())
                .gt("jc_value", 0)
                .list();

        for (JcEvent jcEvent : list) {
            JcEventRightPanelResponse response = new JcEventRightPanelResponse();
            response.setJcEventId(jcEvent.getId());

            KqInfoTable kqInfoTable = kqDao.findById(jcEvent.getKqId()).orElse(null);
            response.setKqName(kqInfoTable.getName());

            response.setJzName(jzDao.findById(jcEvent.getJzId()).orElse(null).getJzName());

            response.setJcdName(jcdDao.findById(jcEvent.getJcdId()).orElse(null).getJcdName());

            response.setJcTime(DateUtil.format(jcEvent.getJcTime(), "yyyy-MM-dd"));

            response.setJcValue(jcEvent.getJcValue());

            responses.add(response);
        }

        return responses;
    }
}
